package com.szqhyxj.youxiangju.biz.plan.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BasePromptFragment;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.creditReport.CreditReportActivity;
import com.szqhyxj.youxiangju.biz.creditReport.demonstration.DemonstrationActivity;
import com.szqhyxj.youxiangju.biz.equipmentActivation.headIdCard.HeadIdCardActivity;
import com.szqhyxj.youxiangju.biz.plan.RepaymentModel;
import com.szqhyxj.youxiangju.biz.plan.adapter.UnBilledAdapter;
import com.szqhyxj.youxiangju.biz.plan.create.PlanCreateActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by leung on 2018/11/4.
 */

public class UnbilledFragment extends BasePromptFragment implements UnBilledAdapter.IconClickListener {


    Unbinder unbinder;
    static ArrayList<RepaymentModel.RepaymentEntity> unbillLists;
    @BindView(R.id.unbilledList)
    RecyclerView billedList;

    @BindView(R.id.none)
    LinearLayout none;
    private UnBilledAdapter unBilledAdapter;

    public static UnbilledFragment newInstance(ArrayList<RepaymentModel.RepaymentEntity> list) {
        Bundle args = new Bundle();
        UnbilledFragment fragment = new UnbilledFragment();
        fragment.setArguments(args);
        unbillLists = list;
        return fragment;
    }

    public void update() {

        isgone();

    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_unbilled;
    }

    @Override
    protected void start() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);

        billedList.setOverScrollMode(View.OVER_SCROLL_NEVER);
        billedList.setLayoutManager(new GridLayoutManager(getContext(), 1));
        unBilledAdapter = new UnBilledAdapter(unbillLists, this);
        billedList.setAdapter(unBilledAdapter);
        isgone();

        return rootView;
    }

    private void isgone() {
        if (unbillLists.size() == 0) {
            billedList.setVisibility(View.GONE);
            none.setVisibility(View.VISIBLE);
        } else {
            none.setVisibility(View.GONE);
            billedList.setVisibility(View.VISIBLE);
            Log.e("aaaaaaaaaa", unbillLists.size() + "");
            unBilledAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onClickItem(int i, List<RepaymentModel.RepaymentEntity> list) {
        RepaymentModel.RepaymentEntity repaymentEntity = list.get(i);
        if (repaymentEntity.getBankStates() == 0) {
            if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {
                HeadIdCardActivity.start(getActivity());
            } else {
             //   CreditReportActivity.start(getActivity(), repaymentEntity.getCardId());
                DemonstrationActivity.start(getActivity());
            }
        }
        if (repaymentEntity.getBankStates() == 1) {

        }
        if (repaymentEntity.getBankStates() == 4) {

        }
        if (repaymentEntity.getBankStates() == 2) {
            if (Integer.parseInt(repaymentEntity.getDueDate()) < 3) {
                Toast.makeText(getActivity(), "还款日小于等于3天不能进行制定计划！", Toast.LENGTH_LONG).show();
                return;
            } else {
                String cardNo = repaymentEntity.getBankCardNo();
                PlanCreateActivity.start(getActivity(), repaymentEntity.getFixedAmount(), repaymentEntity.getBtName() + "(" + cardNo.substring(cardNo.length() - 4, cardNo.length()) + ")", repaymentEntity.getBankCardNo(), Integer.parseInt(repaymentEntity.getBillingDate()), Integer.parseInt(repaymentEntity.getDueDate()), repaymentEntity.getAreainfo());
            }

        }
    }
}